//
//  ViewController.m
//  UIScrollView
//
//  Created by OnePiece on 15/9/9.
//  Copyright (c) 2015年 qianfeng. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIScrollViewDelegate>
{
    UIImage * image;
    UIImageView * imageView;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //拿到图片
    image = [UIImage imageNamed:@"3.png"];
    
    //创建inamgeView 根据图片image的大小定义宽高（bounds）
   imageView=[[UIImageView alloc]initWithFrame:CGRectMake(-20, -20,image.size.width,image.size.height)];
    
    //让imageView现实image
    imageView.image = image;
    
    //与用户交互
    imageView.userInteractionEnabled = YES;
    
    //创建一个滚动试图
    
    UIScrollView * scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
//    self.hidesBottomBarWhenPushed = YES;
//    self.tabBarController.tabBar.hidden = NO;
    
    //显示(相框)内容的大小 默认是scroll本身的大小
    //content(容纳)   size 内容的大小  scroll 可以容纳 image 的大小
    
    scroll.contentSize = CGSizeMake(image.size.width, image.size.height);
    
    //隐藏滚动条  默认是YES(不显示)
    scroll.showsHorizontalScrollIndicator = NO;
    scroll.showsVerticalScrollIndicator = NO;
    
    //scroll的左上角是远点 默认显示从原点开始  contentOffset相对于原点的偏移量
   // scroll.contentOffset = CGPointMake(200, 200);
    
    
    //算出 偏移量 使相框的中心在图片的中心(与下相同的效果)
    scroll.contentOffset=CGPointMake(image.size.width/2-scroll.center.x , image.size.height/2-scroll.center.y);
    
    //相框的中心在 图片的中心
    // imagev.center=scroll.center;
    
    
    
    //默认是YES 到边界时的回弹效果
    
    scroll.bounces = NO;//没有
    
    
    
    //分页效果  默认是NO
     scroll.pagingEnabled = YES;
    //代理
    scroll.delegate = self;
    
    
    //设置缩放
    scroll.minimumZoomScale = 0.5;//最小缩小比
    scroll.maximumZoomScale = 2.0;//最大放大比
    
    
    [scroll addSubview:imageView];
    
    [self.view addSubview:scroll];
}
#pragma mark **在整个图片被拖拽的过程当中一直被调用的方法**

//再用到代理时下面的方法会自动调用，如果你用到那个你就把那个拷贝上去，（如果要写方法就在里面写）

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"我被一直调用着~我的偏移量是x = %f y = %f",imageView.center.x,imageView.center.y);
}
#pragma mark **关于减速的代理方法**
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    NSLog(@"结束减速过程");
}

-(void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"将要开始减速");
}

#pragma mark **关于拖拽的方法(Drag)**

-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    
//    NSLog(@"结束拖拽");
//    UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
//    
//    label.text=@"你弄疼我了";
//    
//    [self.view addSubview:label];
}
-(void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    NSLog(@"将要开始拖拽");
    
    
}
-(void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset{
    
    NSLog(@"将要结束拖拽");
}
#pragma mark **关于滚动到顶部的代理方法**

//这个再程序中被用了
-(BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView{
    
    //是否设置点击状态栏回滚到最顶部 默认为YES
    //如果一个界面存在2个以上的scrollView的时候,这个方法会失灵，解决办法让其中的一个 == YES 另外的都设置为NO 不然会冲突
    return YES;
}
-(void)scrollViewDidScrollToTop:(UIScrollView *)scrollView{
    
    NSLog(@"已经回滚到顶部了");
}


#pragma mark **关于缩放的代理方法**

//返回的参数是指scrollView的哪个子view做缩放
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    return scrollView.subviews[0];
}

-(void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view{
    
    NSLog(@"将要开始缩放");
}
//在缩放过程当中一直被调用
-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
    NSLog(@"开始缩放");
}
-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    
    NSLog(@"结束缩放");
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
